##  Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com 
##
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import os, sys
import shutil



import pytriangulum
import pytriangulum.dev
from  pytriangulum.dev import make, libs



source_files = ["src/vector.cpp", 
                "src/utils.cpp", 
                "src/animate.cpp", 
                "src/node.cpp", 
                "src/timestamp.cpp", 
                "src/matrix.cpp", 
                "src/system.cpp", 
                "src/rectangle.cpp", 
                #"src/transport.cpp", 
                #"src/net.cpp", 
                "src/exception.cpp", 
                "src/xmlnode.cpp", 
                "src/threads.cpp", 
                

                "src/tinyxml/tinystr.cpp", 
                "src/tinyxml/tinyxml.cpp", 
                "src/tinyxml/tinyxmlparser.cpp", 
                "src/tinyxml/tinyxmlerror.cpp"
                ]

tests = ['test_animation', 
         'test_utils', 
         'test_node', 
         'test_timestamp', 
         'test_matrix', 
         'test_xml']
         #'test_tcp']

def MKDIR(d):
    try:
        os.mkdir(d)
    except Exception, e:
        pass
        #print e

def convertTab2Space(src,dst):
    
    fh = open(src)
    data = fh.read();
    fh.close();

    data =  data.replace('\t','    ')
    if data[-1] != '\n':
        data+='\n';
    open(dst,'w').write(data)

def convertTab2Space(DIR):
    MKDIR('noTabs')
    MKDIR(os.path.join('noTabs',DIR))
    l = os.listdir(DIR)
    for f in l:
        if f.endswith('.h') or f.endswith('.cpp'):
            convertTab2Space(DIR+"/"+f,os.path.join('noTabs',DIR,f))
    
def addTriangulumBase(prj, trg=None):
    if sys.platform == 'win32':
        include = [r'C:\\Triangulum\\Base\\include']
        lib_dirs = [r'C:\\Triangulum\\Base\\lib']
        libs = ['TriangulumBase', 'ws2_32']
        cpp = []
        flags = []
        macros = []
        
        prj.add(trg, 
                CPP = cpp, 
                FLAGS = flags, 
                MACRO = macros, 
                INC = include, 
                LIB_DIR = lib_dirs, 
                LIBS = libs)


def create_test(prj, trg):
    MKDIR('release/tests')
    prj.addTarget(trg, 'release/tests/'+trg)
    prj.setMetaData(trg, discript = 'Unit Test ', icon = 'resources/triang.ico')

    prj.add(trg, CPP=['tests/'+trg+'.cpp'])
    addTriangulumBase(prj, trg)
    libs.addPThreads(prj, trg)
    libs.addGLUT(prj, trg)
   

def create_unit_tests(prj):
    create_test(prj, 'test_animation')
    create_test(prj, 'test_utils')
    create_test(prj, 'test_node')
    create_test(prj, 'test_timestamp')
    create_test(prj, 'test_matrix')
    create_test(prj, 'test_xml')
    #create_test(prj, 'test_tcp')

def create_test_app(prj, trg):
    prj.addTarget(trg, 'trg')
    prj.setMetaData(trg, discript = 'demo application ', icon = 'resources/triang.ico')
  
    addTriangulumBase(prj, trg)
    libs.addGLUT(prj, trg)
    prj.add(trg, CPP="main.cpp")
    
def create_main_lib(prj):
    prj.addTarget('lib', 
                  OUT = 'release/lib/TriangulumBase', 
                  TYPE = make.STATIC_LIB)
    prj.add(CPP=source_files)
    
    libs.addGLUT(prj, 'lib')
    libs.addPThreads(prj, 'lib')
    create_test_app(prj, 'demo')
    libs.addGLUT(prj, 'demo')
   
def install():
    print '********** INSTALLING ***********'
    root = 'C:\\Triangulum\\Base'
    MKDIR('C:\\Triangulum')
    MKDIR('C:\\Triangulum\\Base')
    MKDIR(root+'\\include')
    MKDIR(root+'\\include\\tinyxml')
    #MKDIR(root+'\\include\\sigslot')
    MKDIR(root+'\\lib')
    root = 'c:\\Triangulum\\Base'
    os.system('copy \"release\\include\\*.h\" \"'+root+'\\include\"')
    os.system('copy \"release\\include\\tinyxml\\*.h\" \"'+root+'\\include\\tinyxml\"')
    #os.system('copy \"release\\include\\sigslot\\*.h\" \"'+root+'\\include\\sigslot\"')
    os.system('copy \"release\\lib\\*.a\" \"'+root+'\\lib\"')

def main(argv):
    MKDIR('release')
    MKDIR('release/include')
    MKDIR('release/include/tinyxml')
    MKDIR('release/include/sigslot')
    #MKDIR('release/include/sigslot')
    MKDIR('release/lib')
    os.system('copy \"src\\*.h\" release\\include')
    os.system('copy \"src\\tinyxml\\*.h\" release\\include\\tinyxml')
    os.system('copy \"src\\sigslot\\*.h\" release\\include\\sigslot')
    #os.system('copy \"src\\sigslot\\*.h\" release\\include\\sigslot')
    prj = make.Project('make'+sys.platform)
    create_main_lib(prj)
    
    create_unit_tests(prj)
    prj.createGroupTarget('all_tests', tests)
 
    if len(argv) > 1:
        target = argv[1]
        prj.build(target)
##        if target=='lib':
##            install()
    else:
        prj.build('lib')
##        install()
        
    

#=============================================

if __name__ == '__main__':
    
    main(sys.argv)
    
